package com.lwc.cfdns.controller.admin.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lwc.cfdns.constants.ConstantsUrl;
import com.lwc.cfdns.pojo.bo.system.NoticeBO;
import com.lwc.cfdns.pojo.dto.ValidGroup;
import com.lwc.cfdns.pojo.dto.system.NoticeDTO;
import com.lwc.cfdns.pojo.entity.SystemNotice;
import com.lwc.cfdns.pojo.vo.system.NoticeVO;
import com.lwc.cfdns.service.impl.SystemNoticeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * 通知公告
 *
 * @author luwc
 * @title NoticeController
 * @description
 * @time 2023/6/5 14:27:45
 * @version: 1.0
 */
@RestController
@RequestMapping(ConstantsUrl.ADMIN + "/system")
public class NoticeController {
    @Autowired
    private SystemNoticeServiceImpl systemNoticeService;

    /**
     * 创建
     *
     * @param noticeDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/5 14:30:37
     */
    @PreAuthorize("hasAnyAuthority('system:notice:create')")
    @PostMapping("/notice")
    public Map<String, Object> create(@Validated(ValidGroup.Create.class) @RequestBody NoticeDTO noticeDTO) {
        return systemNoticeService.create(noticeDTO);
    }

    /**
     * 修改
     *
     * @param noticeDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/5 14:35:46
     */
    @PreAuthorize("hasAnyAuthority('system:notice:update')")
    @PutMapping("/notice")
    public Map<String, Object> update(@Validated(ValidGroup.Update.class) @RequestBody NoticeDTO noticeDTO) {
        return systemNoticeService.update(noticeDTO);
    }

    /**
     * 详情
     *
     * @param id
     * @return NoticeVO
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/5 14:39:20
     */
    @PreAuthorize("hasAnyAuthority('system:notice:view')")
    @GetMapping("/notice/{id}")
    public NoticeVO view(@PathVariable("id") @NotNull(message = "ID不能为空") Long id) {
        return systemNoticeService.view(id);
    }

    /**
     * 删除(支持批量)
     *
     * @param ids
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/5 14:40:52
     */
    @PreAuthorize("hasAnyAuthority('system:notice:delete')")
    @DeleteMapping("/notice/{ids}")
    public Map<String, Object> delete(@PathVariable("ids") @NotBlank(message = "ids不能为空") String ids) {
        return systemNoticeService.delete(ids);
    }

    /**
     * 列表
     *
     * @param noticeBO
     * @return IPage<SystemNotice>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/5 14:42:47
     */
    @PreAuthorize("hasAnyAuthority('system:notice:lists')")
    @GetMapping("/notice")
    public IPage<SystemNotice> lists(NoticeBO noticeBO) {
        return systemNoticeService.lists(noticeBO);
    }
}
